var DEBUG = true;

function debug(msg) {
	if(DEBUG) {
		$('#debug').append(msg+'<br />');
	}
}

$(document).ready(function() {
	$("#action_profile #response").hide();
	$("#action_profile #templates").hide();

	$("#action_profile #response").load(
		'showProfile',
		function() {
			var strData = $(this).text();
			debug(strData);
			var objData = jQuery.parseJSON(strData);
			if(objData.profile != null) {
				var p = objData.profile;
				$('#action_profile #username').text(p.username);
				$('#action_profile h1').text(p.lastname + ', ' + p.firstname + '\'s profile');
				var c = objData.profile.category;
				for (i in c) {
					$('#action_profile #category_template .category_item').clone().appendTo('#action_profile #category');
					$('#action_profile #category .category_item:last-child').text(c[i]);
				}
			} else if(objData.redirect != null) {
				window.location = objData.redirect;
			} else {
				debug("null profile!");
			}
		}
	);
});